package com.tfy.liquor.service.impl;

import com.tfy.liquor.bean.UserShoppingAddress;
import com.tfy.liquor.dao.FreightRegionDao;
import com.tfy.liquor.dao.UserShoppingAddressDao;
import com.tfy.liquor.service.UserShoppingAddressService;
import com.tfy.liquor.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserShoppingAddressServiceImpl implements UserShoppingAddressService {
    @Autowired
    private FreightRegionDao freightRegionDao;
    @Autowired
    private UserShoppingAddressDao userShoppingAddressDao;

    // 添加收货地址信息
    @Override
    public Boolean addUserShoppingAddress(UserShoppingAddress userShoppingAddress) {
        // 补充省名称
        userShoppingAddress.setProvinceName(freightRegionDao.getFreightRegionById(userShoppingAddress.getProvince()).getName());
        // 补充市名称
        userShoppingAddress.setCityName(freightRegionDao.getFreightRegionById(userShoppingAddress.getCity()).getName());
        // 补充区县名称
        userShoppingAddress.setAreaName(freightRegionDao.getFreightRegionById(userShoppingAddress.getArea()).getName());
        // 补充街道名称以及邮政编码
        if (null != userShoppingAddress.getTown()) {
            userShoppingAddress.setTownName(freightRegionDao.getFreightRegionById(userShoppingAddress.getTown()).getName());
            userShoppingAddress.setAreaCode(freightRegionDao.getFreightRegionById(userShoppingAddress.getTown()).getCode().toString());
        }
        // 如果邮政编码为空，则默认000000
        if (null == userShoppingAddress.getPostalCode()) {
            userShoppingAddress.setPostalCode("000000");
        }
        // 设置创建时间以及更新时间
        userShoppingAddress.setCreateTime(DateUtil.getNowDateTime());
        userShoppingAddress.setUpdateTime(DateUtil.getNowDateTime());
        // 如果是第一次添加
        if (null == userShoppingAddressDao.getDefaultUserShoppingAddress(userShoppingAddress.getUserId())) {
            // 设置为默认地址
            userShoppingAddress.setIsDefault(1);
        } else if (userShoppingAddress.getIsDefault() == 1) { // 如果增地址是默认地址
            // 将其他默认地址设置为非默认
            userShoppingAddressDao.stopUserShoppingIsDefault(userShoppingAddress);
        }
        // 添加地址信息
        return userShoppingAddressDao.addUserShoppingAddress(userShoppingAddress) > 0;
    }

    // 根据用户信息获取收货地址
    @Override
    public ArrayList<UserShoppingAddress> getUserShoppingAddressByUserId(Long userId) {
        return userShoppingAddressDao.getUserShoppingAddressByUserId(userId);
    }

    // 根据收货地址编号获取信息
    @Override
    public UserShoppingAddress getUserShoppingAddressById(int addressId) {
        return userShoppingAddressDao.getUserShoppingAddressById(addressId);
    }

    // 停用收货地址信息
    @Override
    public Boolean stopUserShoppingAddressById(UserShoppingAddress userShoppingAddress) {
        // 设置停用时间
        userShoppingAddress.setUpdateTime(DateUtil.getNowDateTime());
        return userShoppingAddressDao.stopUserShoppingAddressById(userShoppingAddress) > 0;
    }

    // 获取用户默认收货地址
    @Override
    public UserShoppingAddress getDefaultUserShoppingAddress(Long userId) {
        return userShoppingAddressDao.getDefaultUserShoppingAddress(userId);
    }
}
